package com.raysweather.android;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * Tells the parser what tags to look for
 * @author Matthew Lingelbach
 *
 */
public class StationXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static StationList stationList = null;

	public static StationList getSitesList() {
		return stationList;
	}

	public static void setSitesList(StationList sitesList) {
		StationXMLHandler.stationList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("station_info"))
		{
			/** Start */
			stationList = new StationList();
		} 

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("station_id"))
			stationList.setId(currentValue);
		else if (localName.equalsIgnoreCase("station_long_name"))
			stationList.setLongName(currentValue);
		else if (localName.equalsIgnoreCase("station_short_name"))
			stationList.setShortName(currentValue);
		else if (localName.equalsIgnoreCase("city"))
			stationList.setCity(currentValue);
		else if (localName.equalsIgnoreCase("state"))
			stationList.setState(currentValue);
		else if (localName.equalsIgnoreCase("zip"))
			stationList.setZip(currentValue);
		else if (localName.equalsIgnoreCase("latitude"))
			stationList.setLat(currentValue);
		else if (localName.equalsIgnoreCase("longitude"))
			stationList.setLong(currentValue);
		else if (localName.equalsIgnoreCase("elevation"))
			stationList.setElev(currentValue);
		else if (localName.equalsIgnoreCase("last_update"))
			stationList.setLastUpdate(currentValue);
		else if (localName.equalsIgnoreCase("nws_zone_code")){
			stationList.setNWSCode(currentValue);
		}
		else if (localName.equalsIgnoreCase("county"))
			stationList.setCounty(currentValue);
		else if (localName.equalsIgnoreCase("site"))
			stationList.setSite(currentValue);
		else if (localName.equalsIgnoreCase("closest_radar"))
			stationList.setRadar(currentValue);
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
